package br.com.checkandgo;

import java.util.ArrayList;
import java.util.List;

import br.com.checkandgo.model.Constants;

import com.google.android.gms.maps.model.LatLng;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class CaronaActivity extends Activity{

	public ListView caronas;
	public List<ParseObject> rotas;
	public ArrayList<String> nomeRotas = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.carona);

		caronas = (ListView) findViewById(R.id.listView1);

		ParseQuery<ParseObject> query = ParseQuery.getQuery("Rota");
		query.whereEqualTo("idUser", Constants.userLogged.getObjectId());
		query.findInBackground(new FindCallback<ParseObject>() {
			public void done(List<ParseObject> rotasList, ParseException e) {
				rotas = rotasList;
				for(ParseObject obj : rotasList){
					nomeRotas.add(obj.getString("nome"));
				}
				setValuesList();
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.conta, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Intent intent = new Intent(CaronaActivity.this, ContaActivity.class);
			startActivity(intent);
			return true;
		}
		if (id == R.id.home) {
			Intent intent = new Intent(CaronaActivity.this, MenuPrincipalActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void setValuesList(){
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nomeRotas);
		caronas.setAdapter(adapter);

		caronas.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				ParseQuery<ParseObject> query = ParseQuery.getQuery("PontosRota");
				query.whereEqualTo("idRota", rotas.get(position).getObjectId());
				query.findInBackground(new FindCallback<ParseObject>() {

					@Override
					public void done(List<ParseObject> objects, ParseException e) {
						ParseObject pontos = objects.get(0);
						
						List<Double> lats = pontos.getList("latitude");
						List<Double> longs = pontos.getList("longitude");
						
						LatLng posIni = new LatLng(lats.get(0), longs.get(0));
						LatLng posFin = new LatLng(lats.get(lats.size()-1), longs.get(longs.size()-1));
						
						Constants.setFirstPoint(posIni);
						Constants.setLastPoint(posFin);
						
						Intent intent = new Intent(CaronaActivity.this, ListaCaronasActivity.class);
						startActivity(intent);
					}

				});
			}
		});
	}

}
